package com.android.geotrack.artist;

import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * Parses XML data from server.
 * 
 * @author	Laurens Van Keer
 */
public class ArtistParser extends DefaultHandler
{
	private LinkedList<Artist> artists = new LinkedList<Artist>();
	private Boolean tagName = false;
	private Boolean tagUrl = false;
	private Boolean tagThumbnail_small = false;
	private Boolean tagThumbnail_medium = false;
	private Boolean tagThumbnail_large = false;
	private Artist a;

	/**
	* Gets called, whenever a new tag is found.
	*/
	@Override
	public void startElement(String uri, String localName, String name, Attributes attributes)
		throws SAXException
	{
		if (localName.equals("Artist")) {
			a = new Artist();
		}
		
		if (localName.equals("Name")) {
			tagName = true;
		}
		
		if (localName.equals("Url")) {
			tagUrl = true;
		}
		
		if (localName.equals("Thumnail_small")) {
			tagThumbnail_small = true;
		}
		
		if (localName.equals("Thumbnail_medium")) {
			tagThumbnail_medium = true;
		}
		
		if (localName.equals("Thumbnail_large")) {
			tagThumbnail_large = true;
		}
	}

	/**
	* Gets called, whenever a closed tag is found.
	*/
	@Override
	public void endElement(String uri, String localName, String name)
		throws SAXException
	{
		if (localName.equals("Artist")) {
			artists.add(a);
		}
		
		if (localName.equals("Name")) {
			tagName = false;
		}
		
		if (localName.equals("Url")) {
			tagUrl = false;
		}
		
		if (localName.equals("Thumnail_small")) {
			tagThumbnail_small = false;
		}
		
		if (localName.equals("Thumbnail_medium")) {
			tagThumbnail_medium = false;
		}
		
		if (localName.equals("Thumbnail_large")) {
			tagThumbnail_large = false;
		}
	}

	/**
	* Gets called whenever text is found. For example between <Title> and </Title>
	*/
	@Override
	public void characters(char[] ch, int start, int length)
		throws SAXException
	{
		String s = new String(ch,start,length).trim();
		
		if (tagName) {
			a.setName(s);
		}
		
		if (tagUrl) {
			a.setUrl(s);
		}
		
		if (tagThumbnail_small) {
			a.setThumbnail_small(s);
		}
		
		if (tagThumbnail_medium) {
			a.setThumbnail_medium(s);
		}
		
		if (tagThumbnail_large) {
			a.setThumbnail_large(s);
		}
	}

	/**
	* Parses a XML with SAX.
	* Call getParseResult() to get the result.
	*
	* @param xml XML-Code that should be parsed.
	*/
	public void parseXml(String xml)
	{
		try {
			
			// New SAX parser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
		
			// Parse xml file with give XML
			saxParser.parse(new InputSource(new StringReader(xml)), this);
	
		} catch (ParserConfigurationException e) {
			Log.v("ParserConfigurationException", e.toString());
		} catch (SAXException e) {
			Log.v("SAXException", e.toString());
		} catch (IOException e) {
			Log.v("IOException", e.toString());
		}
	}

	/**
	* Returns the result of xml parsing.
	* Call this after calling parseXML().
	*
	* @return	LinkedList
	*/
	public LinkedList<Artist> getParseResult() {
		return artists;
	}
}